<?php include 'head.php';
$sorts = $DB->getAll("SELECT * FROM `sort`");
?>
<title>添加分类</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>

<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-header">添加分类</div>
        <div class="layui-card-body">
            <form class="layui-form" action="" onsubmit="return false">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" autocomplete="off" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">时间限制</label>
                    <div class="layui-input-block">
                        <input type="checkbox" lay-skin="switch" lay-text="开启|关闭">
                    </div>
                </div>
                <div id="settime">
                    <div class="layui-form-item">
                        <label class="layui-form-label">开始时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="begin" autocomplete="off" class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">开始时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="end" autocomplete="off" class="layui-input" value="">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="pear-btn pear-btn-primary" lay-submit lay-filter="formDemo">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-warning">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>
<script src="/component/layui/layui.js"></script>

<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'layer', 'jquery', 'button', 'popup', 'loading', 'laydate'], function() {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            button = layui.button,
            popup = layui.popup,
            loading = layui.loading,
            laydate = layui.laydate;

        laydate.render({
            elem: '#settime input',
            type: 'datetime'
        });

        form.on('switch', function(data) {
            if(data.elem.checked){
                $('#settime').css('display', 'block');
            }else{
                $('#settime').css('display', 'none');
            }
        });

        if($('input[type="checkbox"]')[0].checked){
                $('#settime').css('display', 'block');
            }else{
                $('#settime').css('display', 'none');
            }

        // 修改信息
        window.onkeypress = function(e) {
            if (e.keyCode == 13) {
                $('[lay-submit]').click();
            }
        }
        form.on('submit(formDemo)', function(data) {

            /// 验证
            console.log(data.field);
            if($('input[type="checkbox"]')[0].checked){
                var send = data.field;
            }else{
                var send = {name: data.field.name};
            }

            /// 按钮加载动画
            var load = button.load({
                elem: '.pear-btn-primary'
            });
            
            /// 提交
            $.ajax({
                url: './ajax.php?act=addsort',
                type: "POST",
                timeout: "5000",
                data: send,
                success: function(data) {
                    load.stop();
                    if (data.code == "200") {
                        popup.success(data.msg);
                    } else {
                        popup.failure(data.msg);
                    }
                },
                error: function(XMLResponse) {
                    load.stop();
                    popup.failure("提交异常");
                }
            });

            return false;
        });
    });
</script>

</html>